package com.sanqing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sanqing.dao.base.BaseDao;
import com.sanqing.po.Menber;
import com.sanqing.service.MenberService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;
import com.sanqing.util.PageUtil;
import com.sanqing.util.TimeHelper;

@Service("menberService")
@Transactional(readOnly = true)
public class MenberServiceImpl implements MenberService {

	@Resource(name = "menberDao")
	private BaseDao<Menber, String> menberDao;

	@Override
	public PageResult queryMenberByPage(Page page, Menber Menber) {
		Map<String, Object> map = new HashMap<String, Object>();
		String hql = "from Menber a where 1=1";

		page = PageUtil.createPage(page.getEveryPage(), (int) menberDao.countHqlResult(hql, map), page.getCurrentPage());// 根据总记录数创建分页信息
		List<Menber> list = menberDao.findByPage(page, hql, map);// 通过分页信息取得试题
		PageResult result = new PageResult(page, list);// 封装分页信息和记录信息，返回给调用处
		return result;
	}

	@Override
	@Transactional(readOnly = false)
	public void add(Menber menber) {
		menber.setCreatTime(TimeHelper.getCurrentTime());
		menberDao.save(menber);
	}

	@Override
	public Menber getById(String id) {
		return menberDao.getByKey(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void edit(Menber menber) {
		Menber r = menberDao.getByKey(menber.getId());
		BeanUtils.copyProperties(menber, r,new String[]{"createTime"});
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		menberDao.delete(id);
	}

}
